package com.ran.servers.listener;

import com.ran.servers.ServerConfig;
import com.ran.servers.server.NettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class NettyServerListener implements ServletContextListener {
    private final Logger log = LoggerFactory.getLogger(NettyServerListener.class);

    @Resource
    private ServletContext servletContext;
    /** 注入NettyServer */
    @Resource
    public NettyServer nettyServer;


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("ServletContex初始化...");

        Thread thread = new Thread(new NettyServerThread());
        // 启动netty服务
        thread.start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    /**
     * Netty 服务启动线程
     */
    private class NettyServerThread implements Runnable {

        @Override
        public void run() {
//            if(nettyServer==null)nettyServer=new NettyServer();
            nettyServer.start();
        }
    }
}
